package D01;

import java.io.*;
import java.lang.reflect.Field;
import java.util.Objects;

public class saveObject {

    public static void saveObject(Object obj) throws IOException, IllegalAccessException {
        OutputStream fis=new FileOutputStream("src/D01/objects.txt",true);
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fis));

        Class<?> aClass = obj.getClass();

        Field[] declaredFields = aClass.getDeclaredFields();

        bw.write("======="+aClass.getName()+"=========="+"\r\n");
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object o = field.get(obj);

            bw.write(fieldName+"="+o+"\r\n");
            System.out.println(fieldName+"="+o);
        }
        bw.write("===================================="+"\r\n");
        bw.write("\r\n");
        bw.flush();
    }

    public static void main(String[] args) throws IOException, IllegalAccessException {
        saveObject(new Cat("张三",18,5.5));
        System.out.println("====================");
        saveObject(new Student("李四",18,100,"男"));
    }
}
